home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS05.ADF / IFF / EALstrup.asm < prev    next >
Assembly Source File  |  1986-04-20  |  7KB  |  261 lines

  1.  
  2.    section   text,CODE
  3. * C initial startup procedure under AmigaDOS
  4. * Requirements:
  5.  
  6.    INCLUDE   "exec/types.i"
  7.    INCLUDE "exec/alerts.i"
  8.    INCLUDE "exec/nodes.i"
  9.    INCLUDE "exec/lists.i"
  10.    INCLUDE "exec/ports.i"
  11.    INCLUDE "exec/libraries.i"
  12.    INCLUDE "exec/tasks.i"
  13.    INCLUDE "libraries/dos.i"
  14.    INCLUDE "libraries/dosextens.i"
  15.    INCLUDE "workbench/startup.i"
  16.  
  17. ; some usefull macros:
  18. xlib   macro
  19.    xref   _LVO\1
  20.    endm
  21.  
  22. callsys   macro
  23.    CALLLIB   _LVO\1
  24.    endm
  25.    
  26.  
  27.    xdef   _XCEXIT         * exit(code) is standard way to leave C.
  28.  
  29.    xref   _Debug
  30.    xref   __main         * Name of C program to start with.
  31.    xref   _MemCleanup
  32.    xref   _AbsExecBase
  33.    xref   _FindTask
  34.    xref   _main         * added by robp per EA instructions
  35.    xlib   Alert
  36.    xlib   FindTask
  37.    xlib   Forbid
  38.    xlib   GetMsg
  39.    xlib   OpenLibrary
  40.    xlib   ReplyMsg
  41.    xlib   Wait
  42.    xlib   WaitPort
  43.    xlib   Open
  44.    xlib   Close
  45.    xlib   CurrentDir
  46.  
  47. start:
  48.    move.l   d0,dosCmdLen
  49.    move.l   a0,dosCmdBuf
  50.    move.l   a7,d0         ; save old stack ptr
  51.    movem.l   d1-d6/a0-a6,-(a7)
  52.    move.l   d0,a5
  53.    move.l   _AbsExecBase,a6
  54.    move.l   a6,_SysBase
  55.    move.l   a7,__StackPtr      * Save stack ptr
  56.    clr.l   _WBenchMsg
  57.  
  58. ;------ get the address of our task
  59.    suba.l   a1,a1
  60.    callsys   FindTask
  61.    move.l   d0,a4
  62.  
  63. ;------ are we running as a son of Workbench?
  64.    move.l   pr_CurrentDir(A4),_curdir
  65.    tst.l   pr_CLI(A4)
  66.    beq   fromWorkbench
  67.  
  68. ;=======================================================================
  69. ;====== CLI Startup Code ===============================================
  70. ;=======================================================================
  71.  
  72. fromCLI:
  73.    move.l   a5,D0      ; get top of stack
  74.    sub.l   4(a5),D0   ; compute bottom 
  75.    move.l   D0,__base   ; save for stack checking
  76. ;------   attempt to open DOS library:
  77.    bsr   openDOS
  78.  
  79. ;------ find command name:
  80.    move.l   #start,a0
  81. ;   jsr   _Debug
  82.    clr.l   -(sp)
  83.    jsr   _FindTask
  84.    addq.l   #4,sp
  85.    move.l   d0,a0
  86.    move.l   pr_CLI(a0),a0
  87.    add.l   a0,a0      ; bcpl pointer conversion
  88.    add.l   a0,a0
  89.    move.l   cli_CommandName(a0),a1
  90.    add.l   a1,a1      ; bcpl pointer conversion
  91.    add.l   a1,a1
  92.  
  93. ;------   collect parameters:
  94.    move.l   dosCmdLen,d0      ; get command line length
  95.    moveq.l   #0,d1
  96.    move.b   (a1)+,d1
  97.    move.l   a1,__ProgramName
  98.    add.l   d1,d0         ; add length of command name
  99.    addq.l   #1,d0         ; allow for space after command   
  100.  
  101.    clr.w   -(A7)         ; set null terminator for command line
  102.    addq.l   #1,D0         ; force to even number of bytes
  103.    andi.w   #$fffe,D0      ;(round up)
  104.    sub.l   D0,A7         ; make room on stack for command line
  105.    subq.l   #2,D0
  106.    clr.w   0(A7,D0)
  107.  
  108. ;------ copy command line onto stack
  109.    move.l   dosCmdLen,d0      ; get command line length
  110.    move.l   dosCmdBuf,a0
  111.    move.l   d0,d2
  112.    subq.l   #1,d0
  113.    add.l   d1,d2
  114.  
  115. copy_line:
  116.    move.b   0(A0,D0.W),0(A7,D2.W)   ; copy command line to stack
  117.    subq.l   #1,d2
  118.    dbf   d0,copy_line
  119.    move.b   #' ',0(a7,d2.w)      ; add space between command and parms
  120.    subq.l   #1,d2
  121.  
  122. copy_cmd:
  123.    move.b   0(a1,d2.w),0(a7,d2.w)   ; copy command name to stack
  124.    dbf   d2,copy_cmd
  125.    move.l   A7,A1
  126.    move.l   A1,-(A7)      ; push command line address
  127.    jsr   __main              * call C entrypoint
  128.    moveq.l   #0,d0         ; set successful status
  129.    bra.s   exit2
  130.  
  131. ;=======================================================================
  132. ;====== Workbench Startup Code =========================================
  133. ;=======================================================================
  134.  
  135. fromWorkbench:
  136.  
  137.    move.l   TC_SPLOWER(a4),__base   ; set base of stack
  138. ;------ open the DOS library:
  139.    bsr   openDOS
  140.  
  141. ;------ we are now set up.  wait for a message from our starter
  142.    bsr   waitmsg
  143.    move.l   d0,_WBenchMsg
  144.    move.l   d0,-(SP)
  145. ;
  146.    move.l   d0,a2         ; get first argument
  147.    move.l   sm_ArgList(a2),d0
  148.    beq.s   do_cons
  149.    move.l   _DOSBase,a6
  150.    move.l   d0,a0
  151.    move.l   wa_Lock(a0),d1
  152.    callsys   CurrentDir
  153. do_cons:
  154.    move.l   sm_ToolWindow(a2),d1   ; get the window argument
  155.    beq.s   do_main
  156.    move.l   #MODE_OLDFILE,d2
  157.    callsys   Open
  158.    move.l   d0,stdin
  159.    beq.s   do_main
  160.    lsl.l   #2,d0
  161.    move.l   d0,a0
  162.    move.l   fh_Type(a0),pr_ConsoleTask(A4)
  163. do_main:
  164.    clr.l   -(sp)      *was pea _NULL
  165.    jsr   _main              * call C entrypoint (was jsr __main)
  166.    moveq.l   #0,d0         ; set successful status
  167.    bra.s   exit2
  168. ;
  169.  
  170. _XCEXIT:
  171.    move.l   4(SP),d0   ; extract return code
  172. exit2:
  173.    move.l   d0,-(a7)
  174.    jsr   _MemCleanup      ; cleanup leftover memory alloc.
  175.  
  176. ;------ if we ran from CLI, skip workbench cleanup:
  177.    tst.l   _WBenchMsg
  178.    beq.s   exitToDOS
  179.    move.l   _console_dev,d1
  180.    beq.s   done_2
  181.    callsys   Close
  182. done_2:
  183.    move.l   stdin,d1
  184.    beq.s   done_4
  185.    callsys   Close
  186. done_4:
  187.  
  188. ;------ return the startup message to our parent
  189. ;   we forbid so workbench can't UnLoadSeg() us
  190. ;   before we are done:
  191.    move.l   _AbsExecBase,A6
  192.    callsys Forbid
  193.    move.l   _WBenchMsg,a1
  194.    callsys   ReplyMsg
  195.  
  196. ;------ this rts sends us back to DOS:
  197. exitToDOS:
  198.    MOVE.L   (A7)+,D0
  199.    movea.l  __StackPtr,SP      * restore stack ptr
  200.    movem.l   (a7)+,d1-d6/a0-a6
  201.    rts            * and exit
  202.  
  203. ;-----------------------------------------------------------------------
  204. noDOS:
  205.       ALERT   (AG_OpenLib!AO_DOSLib)
  206.       moveq.l   #100,d0
  207.       bra.s   exit2
  208.  
  209.  
  210. ;-----------------------------------------------------------------------
  211. ; This routine gets the message that workbench will send to us
  212. ; called with task id in A4
  213.  
  214. waitmsg:
  215.       lea   pr_MsgPort(A4),a0     * our process base
  216.       callsys   WaitPort
  217.       lea   pr_MsgPort(A4),a0     * our process base
  218.       callsys GetMsg
  219.       rts
  220.  
  221. ;-----------------------------------------------------------------------
  222. ;  Open the DOS library:
  223.  
  224. openDOS
  225.       lea   DOSName,A1
  226.       moveq.l   #0,D0
  227.       callsys OpenLibrary
  228.       move.l   D0,_DOSBase
  229.       beq   noDOS
  230.       rts
  231.  
  232.    section   data,DATA
  233. ;
  234.    XDEF   _NULL,_DOSBase,_SysBase,_LoadAddress,_console_dev,_WBenchMsg
  235.    XDEF   __mbase,__mnext,__msize,__tsize,__oserr,__fperr,__mstep
  236.    XDEF   __ProgramName,__StackPtr,__base
  237. ;
  238. _NULL   DC.L   0         ;
  239. __base   DC.L   0         ; base of stack
  240. __mbase   DC.L   0         ; base of memory pool
  241. __mnext   DC.L   0         ; next available memory location
  242. __msize   DC.L   0         ; size of memory pool
  243. __mstep   DC.L   1024         ; memory pool step size
  244. __tsize   DC.L   0         ; total size?
  245. __oserr   DC.L   0
  246. __fperr   DC.L   0
  247. _curdir   DC.L   0
  248. _console_dev DC.L 0
  249. _SysBase     DC.L 0
  250. _DOSBase     DC.L 0   
  251. _LoadAddress DC.L 0         ; program load address
  252. _WBenchMsg   dc.l   0
  253. __StackPtr   dc.l   0
  254. dosCmdLen   dc.l   0
  255. dosCmdBuf   dc.l   0
  256. stdin   dc.l   0
  257. __ProgramName   dc.l   0
  258. DOSName    DOSNAME
  259.  
  260.